﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using HGZP.BLL;
using HGZP.DAL;
using HGZP.Model;
using HGZP.PLL;

namespace SZCB.admin.FoodProp
{
    public partial class propseal : System.Web.UI.Page
    {
        string g_strFoodGuid = "";
        string g_strFoodName = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["sGuid"] == null || Request.QueryString["sGuid"] == "")
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "温馨提示", "<script language='javascript'>alert('您输入了非法路径！');history.back();</script>");
                }
                else
                {
                    g_strFoodGuid = Request.QueryString["sGuid"].ToString();
                    g_strFoodName = Request.QueryString["sName"].ToString();
                    InitData();

                }
            }
            getCount();
            BindData();

        }

        protected void InitData()
        {
            idFoodName.Text = g_strFoodName;

            CSealEnterpriseBLL myCSealEnterpriseBLL = new CSealEnterpriseBLL();
            ddlContentClass.DataSource = myCSealEnterpriseBLL.F02_SelectHaveWhere("");
            ddlContentClass.DataTextField = myCSealEnterpriseBLL.sName;
            ddlContentClass.DataValueField = myCSealEnterpriseBLL.sGuid;
            ddlContentClass.DataBind();
            ListItem listItem = new ListItem();
            listItem.Text = "----请选择销售商----";
            listItem.Value = "0";
            ddlContentClass.Items.Add(listItem);
            ddlContentClass.SelectedValue = "0";


        }

        private void getCount()
        {
            string sql = "sFoodGuid='" + g_strFoodGuid + "'";
            CFoodSealPriceBLL myCFoodSealPriceBLL = new CFoodSealPriceBLL();
            returnDataPager.RecordCount = myCFoodSealPriceBLL.F09_GetCount(sql);
        }

        private void BindData()
        {
            string sql = "sFoodGuid='" + g_strFoodGuid + "'";
            CFoodSealPriceBLL myCFoodSealPriceBLL = new CFoodSealPriceBLL();
            RptData.DataSource = myCFoodSealPriceBLL.F08_SelectColumnHaveWhereOrder_OnePage(returnDataPager.StartRecordIndex - 1, returnDataPager.PageSize, "*", sql, "");
            RptData.DataBind();
        }

        protected void returnDataPager_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void ddlContentClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlContentClass.SelectedValue == "0")
                return;
            CFoodSealPriceBLL myCFoodSealPriceBLL = new CFoodSealPriceBLL();
            DataTable dt = myCFoodSealPriceBLL.F02_SelectHaveWhere("sFoodGuid='" + g_strFoodGuid + "' and sSealGuid='" + ddlContentClass.SelectedValue + "'");
            if (dt.Rows.Count > 0)
            {
                CFoodSealPrice myCFoodSealPrice = myCFoodSealPriceBLL.F06_SelectOneEntityByGuid(dt.Rows[0]["sGuid"].ToString());
                if (null != myCFoodSealPrice)
                {
                    txtNutrient.Text = myCFoodSealPrice.dPrice.ToString();
                }
                else
                {
                    txtUnit.Text = "";
                    txtNutrient.Text = "";
                }

            }

        }

        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            CFoodSealPriceBLL myCFoodSealPriceBLL = new CFoodSealPriceBLL();
            DataTable dt = myCFoodSealPriceBLL.F02_SelectHaveWhere("sFoodGuid='" + g_strFoodGuid + "' and sSealGuid='" + ddlContentClass.SelectedValue + "'");
            if (dt.Rows.Count > 0)
            {
                CFoodSealPrice myCFoodSealPrice = myCFoodSealPriceBLL.F06_SelectOneEntityByGuid(dt.Rows[0]["sGuid"].ToString());
                myCFoodSealPrice.dPrice = Convert.ToDecimal(txtNutrient.Text);
                if (myCFoodSealPriceBLL.F_Update(myCFoodSealPrice))
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "温馨提示", "<script language='javascript'>alert('营养修改成功！');</script>");
                    getCount();
                    BindData();

                }


            }
            else
            {
                CFoodSealPrice myCFoodSealPrice = new CFoodSealPrice();
                myCFoodSealPrice.sGuid = MyFunction.GetNewGuid();
                myCFoodSealPrice.sFoodGuid = Request.QueryString["sGuid"].ToString();
                myCFoodSealPrice.sSealGuid = ddlContentClass.SelectedValue;
                myCFoodSealPrice.dPrice = Convert.ToDecimal(txtNutrient.Text);
                myCFoodSealPrice.sFoodName = Request.QueryString["sName"].ToString();
                myCFoodSealPrice.sSealName = ddlContentClass.SelectedItem.Text;
                if (myCFoodSealPriceBLL.F_Insert(myCFoodSealPrice))
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "温馨提示", "<script language='javascript'>alert('营养添加成功！');</script>");
                    getCount();
                    BindData();

                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "温馨提示", "<script language='javascript'>alert('营养添加失败！');history.back();</script>");

                }

            }

        }

    }


}